//
//  HTHomePageViewController.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/26.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTHomePageViewController.h"
#import "HTGoodsSourceViewController.h"
#import "HTOrderLinesViewController.h"
#import "HTMineViewController.h"
#import "HTLoginVC.h"
#import "UIView+Layout.h"

@interface HTHomePageViewController ()
@property (nonatomic,strong) HTGoodsSourceViewController *goodsSource;
@property (nonatomic,strong) HTOrderLinesViewController *orderLines;
@property (nonatomic,weak) UIViewController *currentVC;
@end

@implementation HTHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configNav];
    [self configSubViews];
}

#pragma mark --- 绘制界面
- (void)configSubViews{
    self.orderLines = [[HTOrderLinesViewController alloc]init];
    self.orderLines.view.frame = CGRectMake(0, 0, kHTScreenWidth, kHTScreenHeight);
    [self addChildViewController:self.orderLines];
    self.goodsSource = [[HTGoodsSourceViewController alloc]init];
    self.goodsSource.view.frame = CGRectMake(0, 0, kHTScreenWidth, kHTScreenHeight);
    [self addChildViewController:self.goodsSource];
    //设置默认控制器为fristVc
    self.currentVC = self.goodsSource;
    [self.view addSubview:self.goodsSource.view];
}
- (void)configNav{
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"homeIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(popCenter)];
    self.navigationItem.leftBarButtonItem = left;
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"订阅线路",@"货源搜索"]];
    [seg setSelectedSegmentIndex:1];
    [seg addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    seg.backgroundColor = [UIColor clearColor];
    seg.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = seg;
 
}
- (void)popCenter{
    if ([HTMUserDefaultUtil isLogin]) {
        HTMineViewController *mine = [[HTMineViewController alloc]init];
        [self.navigationController pushViewController:mine animated:YES];
    }else{
        HTLoginVC *login = [[HTLoginVC alloc]init];
         [self.navigationController pushViewController:login animated:YES];
    }
    
}
- (void)segmentedControlAction:(UISegmentedControl *)sender{
    switch (sender.selectedSegmentIndex) {
        case 0:
            if ([HTMUserDefaultUtil isLogin]) {
                 [self replaceFromOldViewController:self.goodsSource toNewViewController:self.orderLines];
            }else{
                HTLoginVC *login = [[HTLoginVC alloc]init];
                [self.navigationController pushViewController:login animated:YES];
                sender.selectedSegmentIndex = 1;
            }
            break;
        case 1:
            [self replaceFromOldViewController:self.orderLines toNewViewController:self.goodsSource];
            break;
        default:
            break;
    }
}
- (void)replaceFromOldViewController:(UIViewController *)oldVc toNewViewController:(UIViewController *)newVc{
    [self addChildViewController:newVc];
    [self transitionFromViewController:oldVc toViewController:newVc duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newVc didMoveToParentViewController:self];
            [oldVc willMoveToParentViewController:nil];
            [oldVc removeFromParentViewController];
            self.currentVC = newVc;
        }else{
            self.currentVC = oldVc;
        }
    }];
}
@end
